package com.example.demo3network;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import org.w3c.dom.Text;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "MainActivity";
    private TextView textView;
    private Button networkBtn;
    private Button handlerBtn;
    private Button countDownBtn;
    private Button diglettBtn;
    private Button downloadProgressBtn;
    private Button requestDataBtn;


    Handler handler;

    @SuppressLint("HandlerLeak")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViews();
        setListeners();

        initHandler();

    }

    private void initHandler() {
        HandlerThread handlerThread = new HandlerThread("MyHandlerThread");
        handlerThread.start();

        Looper looper = handlerThread.getLooper();
        handler = new Handler(looper) {
            @Override
            public void handleMessage(@NonNull Message msg) {
                super.handleMessage(msg);
                Log.d(TAG, "handlerMessage: " + msg.toString());
                if (msg.what == 1001) {
                    textView.setText("handlerMessage");
                }
            }
        };
    }

    private void findViews() {
        textView = findViewById(R.id.textView);
        networkBtn = findViewById(R.id.networkBtn);
        handlerBtn = findViewById(R.id.handlerBtn);
        countDownBtn = findViewById(R.id.countDownTime);
        diglettBtn = findViewById(R.id.diglett);
        downloadProgressBtn = findViewById(R.id.downloadProgress);
        requestDataBtn = findViewById(R.id.requestDataBtn);
    }

    private void setListeners() {
        networkBtn.setOnClickListener(this);
        handlerBtn.setOnClickListener(this);
        countDownBtn.setOnClickListener(this);
        diglettBtn.setOnClickListener(this);
        downloadProgressBtn.setOnClickListener(this);
        requestDataBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.networkBtn:
                Intent intent = new Intent(this, NetworkActivity.class);
                startActivity(intent);
                break;
            case R.id.handlerBtn:
                Log.d(TAG, "imooc");
                //handler.sendEmptyMessage(1001);
                Message message = Message.obtain();
                message.what = 1001;
                message.arg1 = 1003;
                handler.sendMessage(message);
                break;
            case R.id.countDownTime:
                Intent intentCountDownTime = new Intent(this, CountDownTimeActivity.class);
                startActivity(intentCountDownTime);
                break;
            case R.id.diglett:
                Intent intentDiglett = new Intent(this, DiglettActivity.class);
                startActivity(intentDiglett);
                break;
            case R.id.downloadProgress:
                Intent intentDownload = new Intent(this, DownloadProgerssActivity.class);
                startActivity(intentDownload);
                break;
            case R.id.requestDataBtn:
                Intent intentRequestData = new Intent(this, RequestDataActivity.class);
                startActivity(intentRequestData);
                break;
        }
    }

    public void onBackgroundClick(View view) {
        //Intent intent = new Intent(this, NetworkActivity.class);
        //startActivity(intent);
        //overridePendingTransition(R.anim.slide_in_from_right, R.anim.slide_in_from_left);
    }






}